package yl.hs.bmipfull.service.system.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.system.IMenuButtonDao;
import yl.hs.bmipfull.pojo.system.MenuButtonEntity;
import yl.hs.bmipfull.service.system.IMenuButtonService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;

import java.util.List;
import java.util.Map;

/**
 * 作用:  菜单按钮服务层接口实现<br/>
 * 创建时间: 2021年05月12日 09时46分52秒 <br/>
 *
 * @author 张凯
 */
@Service
public class MenuButtonServiceImpl extends SystemBaseService implements IMenuButtonService {

    @Autowired
    IMenuButtonDao dao;

    @Override
    public List<MenuButtonEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(MenuButtonEntity.class);
        return dao.queryList(pageQuery.getQuery());
    }

    @Override
    public MenuButtonEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }


    @Transactional
    public Integer saveMenuButton(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;

        if (Tl.isEmpty(sn)) {
            //执行保存
            MenuButtonEntity entity = new MenuButtonEntity();
            entity.setState((short) 0);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(MenuButtonEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入菜单按钮信息", Tl.toJson(entity), MenuButtonEntity.class);
            }

        } else {
            //执行修改
            MenuButtonEntity entity = dao.findBySN(sn);
            var backJson = entity.toJson();
            var changeLog = Tl.getChangeLog(entity, model);

            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, changeLog, backJson, MenuButtonEntity.class);
            }
        }

        return result;
    }

    @Override
    public List<MenuButtonEntity> queryByMenuSn(String menuSn) {
        return dao.queryByMenuSn(menuSn);
    }

    @Transactional
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySn(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, MenuButtonEntity.class);
        }
        return result;
    }
}